//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.northpool.operator.common.algorithm;

import java.io.File;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.Map;

public abstract class AlgorithmBase extends Helper implements Algorithm {
    protected Map<String, String> parameter = new LinkedHashMap();
    protected String hint = "参数必须是偶数";

    public AlgorithmBase() {
    }

    protected boolean getBool(String name, boolean def) {
        return getBool(this.parameter, name, def);
    }

    protected boolean getBool(String name) {
        return this.getBool(name, false);
    }

    protected String getStr(String name, String def) {
        return getStr(this.parameter, name, def);
    }

    protected String getStr(String name) {
        return this.getStr(name, "");
    }

    protected boolean emptyStr(String v) {
        return v == null || "".equals(v.trim());
    }

    protected void error(int status, String msg) {
        this.logError(status, msg);
    }

    protected void error(String msg) {
        this.error(1, msg);
    }

    public String getHint() {
        return this.hint;
    }

    protected void trimStringBuilder(StringBuilder sb, String spliter) {
        if (sb.length() > 1) {
            sb.delete(0, spliter.length());
        }

    }

    public void execute(Map<String, String> params) throws Exception {
        long start = Calendar.getInstance().getTimeInMillis();
        this.logMsg(this.getName() + "算法启动！");
        // 打印java版本
        this.logMsg("Java版本：" + System.getProperty("java.version"));
        try {
            this.parameter.putAll(params);
            this.execute();
            this.logMsg("节点【" + this.getName() + "】执行完成！");
        } catch (Exception var8) {
            this.logInfo("<result><state>1</state><msg>" + var8.getMessage() + "<msg></result>");
            throw var8;
        } finally {
            this.logMsg("执行耗时：" + (Calendar.getInstance().getTimeInMillis() - start) + "毫秒");
            this.dispose();
        }

    }

    protected String abs2Logic(File fileName) {
        String fl = fileName.getAbsolutePath();
        String perfixPathes = this.getStr("perfixPathes");
        if (!this.emptyStr(perfixPathes)) {
            String[] kvs = perfixPathes.split(";");

            for(int i = 0; i < kvs.length; ++i) {
                String[] kv = kvs[i].split("=");
                if (kv.length >= 2 && fl.startsWith(kv[0])) {
                    return this.abs2Logic(fl, kv[1], kv[0]);
                }
            }

            return fl;
        } else {
            return this.abs2Logic(fl, this.getStr("perfix"), this.getStr("perfixPath", this.getStr("tempPath")));
        }
    }

    protected String abs2Logic(String f, String perfix, String perfixPath) {
        f = f.replace("\\", "/").replace("//", "/");
        int p = !perfixPath.endsWith("/") && !perfixPath.endsWith("\\") ? perfixPath.length() + 1 : perfixPath.length();
        return "?{" + perfix + "}/" + f.substring(p);
    }

    protected void execute() throws Exception {
    }

    public class LambdaResult<T> {
        public T value;

        public LambdaResult(T def) {
            this.value = def;
        }

        public boolean isNull() {
            return this.value == null;
        }
    }
}
